<?php
/**
 * Created by PhpStorm.
 * User: 李京蔚
 * Date: 2018/5/24
 * Time: 17:11
 */

namespace App\Http\Controllers\Common;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class FileUploadController extends ApiController
{

    /**
     * 功能：pdf文件上传方法
     * @param Request $request
     * @return mixed
     */
    public function uploadFile(Request $request)
    {
        if ($request->isMethod('post')) {
            $img = $request->file('file');
            if (!$img) {
                return $this->setStatusCode(412)->message('请上传文件！');
            }
            // 文件是否上传成功
            if ($img->isValid()) {
                // 获取后缀名
//                $ext = $img->extension();
                $suffix = $img->getClientOriginalExtension();
                $name = $img->getClientOriginalName();
                $realPath = $img->getRealPath();   //临时文件的绝对路径
                $format = [
                    'pdf'
                ];
                if (!\in_array($suffix, $format)) {
                    return $this->setStatusCode(400)->failed('上传失败,类型只能为PDF文档！');
                }
                // 新文件名
                $saveName = date('Y-m-d', time()) . '/' . date('YmdHis') . uniqid() . '.' . $suffix;
                // 使用 store 存储文件
                $path = Storage::disk('user')->put($saveName, file_get_contents($realPath));
                $data['url'] = '/storage/uploads/' . $saveName;
                $data['name'] = $name;
                if (!$path)
                    return $this->setStatusCode(400)->failed('上传失败！');
                else
                    return $this->success($data);
            }
        }
        return $this->setStatusCode(400)->failed('上传失败！');
    }

    /**
     * 功能：Excel文件上传方法
     * @param Request $request
     * @return mixed
     */
    public function uploadExcel(Request $request)
    {
        if ($request->isMethod('post')) {
            $img = $request->file('file');
            if (!$img) {
                return $this->setStatusCode(412)->message('请上传文件！');
            }
            // 文件是否上传成功
            if ($img->isValid()) {
                // 获取后缀名
//                $ext = $img->extension();
                $suffix = $img->getClientOriginalExtension();
                $name = $img->getClientOriginalName();
                $realPath = $img->getRealPath();   //临时文件的绝对路径
                $format = [
                    'xls', 'xlsx'
                ];
                if (!\in_array($suffix, $format)) {
                    return $this->setStatusCode(400)->failed('上传失败,类型只能为Excel文档！');
                }
                // 新文件名
                $saveName = date('Y-m-d', time()) . '/' . date('YmdHis') . uniqid() . '.' . $suffix;
                // 使用 store 存储文件
                $path = Storage::disk('user')->put($saveName, file_get_contents($realPath));
                $data['url'] = '/storage/uploads/' . $saveName;
                $data['name'] = $name;
                if (!$path)
                    return $this->setStatusCode(400)->failed('上传失败！');
                else
                    return $this->success($data);
            }
        }
        return $this->setStatusCode(400)->failed('上传失败！');
    }

    /**
     * 不做判断的文件上传
     * @param Request $request
     * @return mixed
     */
    public function file(Request $request)
    {
        if ($request->isMethod('post')) {
            $img = $request->file('file');
            if (!$img) {
                return $this->setStatusCode(412)->message('请上传文件！');
            }
            // 文件是否上传成功
            if ($img->isValid()) {
                // 获取后缀名
//                $ext = $img->extension();
                $suffix = $img->getClientOriginalExtension();
                $name = $img->getClientOriginalName();
                $realPath = $img->getRealPath();   //临时文件的绝对路径
                // 新文件名
                $saveName = date('Y-m-d', time()) . '/' . date('YmdHis') . uniqid() . '.' . $suffix;
                // 使用 store 存储文件
                $path = Storage::disk('user')->put($saveName, file_get_contents($realPath));
                $data['url'] = '/storage/uploads/' . $saveName;
                $data['name'] = $name;
                if (!$path)
                    return $this->setStatusCode(400)->failed('上传失败！');
                else
                    return $this->success($data);
            }
        }
        return $this->setStatusCode(400)->failed('上传失败！');
    }

    /**
     * 功能：图片上传方法
     * @param Request $request
     * @return mixed
     */
    public function upload(Request $request)
    {

        if ($request->isMethod('post')) {
            $img = $request->file('file');
            if (!$img) {
                return $this->setStatusCode(412)->message('请上传图片文件！');
            }
            // 文件是否上传成功
            if ($img->isValid()) {
                $type = $img->getClientMimeType();     // image/jpeg
                $realPath = $img->getRealPath();   //临时文件的绝对路径
                if (!$this->is_image($type)) {
                    return $this->setStatusCode(412)->message('请上传正确的图片格式文件！');
                }
                // 获取后缀名
                $ext = $img->extension();
                // 新文件名
                $saveName = date('Y-m-d', time()) . '/' . date('YmdHis') . uniqid() . '.' . $ext;
                // 使用 store 存储文件
//                $path = $img->storeAs(date('Y-m-d'),$saveName);
                $bool = Storage::disk('user')->put($saveName, file_get_contents($realPath));
                $data['url'] = url('/storage/uploads/' . $saveName);//app_path('/storage/uploads/' . $saveName);
                if (!$bool)
                    return $this->failed('上传失败！');
                else
                    return $this->success($data);
            }
        }
        return $this->failed('上传失败！');
    }
}
